﻿using FreeSql;
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace App.DAL
{
    public enum MyEnum
    {

    }
    [Index(fields: "InUsed asc, CreateDt desc", name: "entityBase_Index")]
    public class EntityBase<T> :IExport, ISnowflakeID where T : class, ISnowflakeID
    {
        [Column(IsIdentity = true, IsPrimary = true, Position = 9999)]
        public string ID { get; set; } = Guid.NewGuid().ToString("N");

        [Column(Position = -9998)]
        public bool InUsed { get; set; } = true;

        [Column(Position = -9999)]
        public DateTime CreateDt { get; set; } = DateTime.Now;



        //---------------------------------------------
        // 数据集
        //---------------------------------------------


        public static DbContext Db { get { return DAL.AppContext.Current; } }
        public static DbSet<T> Set { get { return Db.Set<T>(); } }






        //---------------------------------------------
        // 成员方法
        //---------------------------------------------
        /// <summary>删除</summary>
        public void Delete(bool log = true)
        { 
            Set.Remove(x => x.ID == this.ID); 
            //Log(log, LogType.Delete, id: this.ID, data: this);
        }
         
        public void Insert(bool log = true)
        {
            Set.Add(this as T);
            Db.SaveChanges();
            //Log(log, isNew ? LogType.Insert : LogType.Update, id: this.ID, data: this);
        }
        public void Update(bool log = true)
        {
            Set.Update(this as T);
            Db.SaveChanges();
            //Log(log, isNew ? LogType.Insert : LogType.Update, id: this.ID, data: this);
        }

        public virtual object Export(object obj=null)
        {
            return new { this.ID, this.CreateDt, this.InUsed };
        }
    }
}
